package com.llmm.eduService.service.impl;

import com.llmm.eduService.entity.UserMember;
import com.llmm.eduService.entity.vo.RegisterVo;
import com.llmm.eduService.mapper.UserMemberMapper;
import com.llmm.eduService.service.UserCenterService;
import com.llmm.exception.CustomException;
import com.llmm.utils.JwtUtils;
import com.llmm.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

import static com.llmm.exception.CommonErrorCode.*;

@Service
public class UserCenterServiceImpl implements UserCenterService {

    @Autowired
    private UserMemberMapper memberMapper;

    @Override
    public String login(UserMember member) {
        // 获取手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            //跑异常。
            throw new CustomException(E_100002);
        }
        // 判断手机号是否正确。
        UserMember userMember = new UserMember();
        userMember.setMobile(mobile);
        UserMember user = memberMapper.getUser(userMember);
        //判断查询对象是否为空
        if (user == null) {
            throw new CustomException(E_110001);
        }
        // 判断密码。
        if (!MD5Util.getMd5(password).equals(user.getPassword())){
            throw new CustomException(E_100115);
        }
        // 判断用户是否被禁用。
        if (user.getIsDeleted()){
            throw new CustomException(E_110000);
        }
        //登录成功。生成jwtToken字符串。
        String jwtToken = JwtUtils.getJwtToken(user.getId(), user.getNickname());
        return jwtToken;
    }

    @Override
    public Boolean register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickName();
        String password = registerVo.getPassword();

        //非空判断
        if (StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(nickname)
                || StringUtils.isEmpty(password)) {
            throw new CustomException(E_100002);
        }

        // 进行redis判断。
        //判断手机号是否重复，表里面存在相同手机号不进行添加
        UserMember userMember = new UserMember();
        userMember.setMobile(mobile);
        UserMember user = memberMapper.getUser(userMember);
        if (user!=null){
            throw new CustomException(E_100113);
        }
        // 保存用户。
        userMember.setId(UUID.randomUUID().toString());
        userMember.setNickname(nickname);
        userMember.setPassword(MD5Util.getMd5(password));
        userMember.setIsDeleted(false);
        userMember.setIsDisabled(false);
        Integer integer = memberMapper.saveUser(userMember);
        return integer>0;
    }

    @Override
    public UserMember getUserInfo(HttpServletRequest request) {
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        UserMember userMember = new UserMember();
        userMember.setId(userId);
        UserMember user = memberMapper.getUser(userMember);
        return user;
    }
}
